package com.ziven.common.widget.value;

import android.view.View;

import androidx.annotation.DimenRes;
import androidx.annotation.NonNull;

import com.ziven.common.widget.WidgetUtils;
import com.ziven.easygo.util.LogHelper;
import com.ziven.easygo.util.ResourceUtils;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class Padding extends MixValue {

    private static final String TAG = "PaddingLog";

    public static Padding topDimenId(@DimenRes int topDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId)
        );
    }

    public static Padding bottomDimenId(@DimenRes int bottomDimenId) {
        return size(
                DimenValue.bottomDimenId(bottomDimenId)
        );
    }

    public static Padding topBottomDimenId(@DimenRes int paddingDimenId) {
        int dimen = ResourceUtils.getDimen(paddingDimenId);
        return topBottom(dimen, dimen);
    }

    public static Padding topBottomDimenId(@DimenRes int topDimenId,
                                           @DimenRes int bottomDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId),
                DimenValue.bottomDimenId(bottomDimenId)
        );
    }

    public static Padding startDimenId(@DimenRes int startDimenId) {
        return size(
                DimenValue.startDimenId(startDimenId)
        );
    }

    public static Padding endDimenId(@DimenRes int endDimenId) {
        return size(
                DimenValue.endDimenId(endDimenId)
        );
    }

    public static Padding startEndDimenId(@DimenRes int paddingDimenId) {
        int dimen = ResourceUtils.getDimen(paddingDimenId);
        return startEnd(dimen, dimen);
    }

    public static Padding startEndDimenId(@DimenRes int startDimenId,
                                          @DimenRes int endDimenId) {
        return size(
                DimenValue.startDimenId(startDimenId),
                DimenValue.endDimenId(endDimenId)
        );
    }

    public static Padding dimenId(@DimenRes int dimenId) {
        int dimen = ResourceUtils.getDimen(dimenId);
        return size(dimen, dimen, dimen, dimen);
    }

    public static Padding dimenId(@DimenRes int topDimenId,
                                  @DimenRes int bottomDimenId,
                                  @DimenRes int startDimenId,
                                  @DimenRes int endDimenId) {
        return size(
                DimenValue.topDimenId(topDimenId),
                DimenValue.bottomDimenId(bottomDimenId),
                DimenValue.startDimenId(startDimenId),
                DimenValue.endDimenId(endDimenId)
            );
    }

    public static Padding top(@NonNull String top) {
        return size(
                DimenValue.topSize(top)
        );
    }

    public static Padding bottom(@NonNull String bottom) {
        return size(
                DimenValue.bottomSize(bottom)
        );
    }

    public static Padding topBottom(@NonNull String padding) {
        int dimen = WidgetUtils.dimenInt(padding);
        return topBottom(dimen, dimen);
    }

    public static Padding topBottom(@NonNull String top,
                                    @NonNull String bottom) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom)
        );
    }

    public static Padding start(@NonNull String start) {
        return size(
                DimenValue.startSize(start)
        );
    }

    public static Padding end(@NonNull String end) {
        return size(
                DimenValue.endSize(end)
        );
    }

    public static Padding startEnd(@NonNull String padding) {
        int dimen = WidgetUtils.dimenInt(padding);
        return startEnd(dimen, dimen);
    }

    public static Padding startEnd(@NonNull String start,
                                   @NonNull String end) {
        return size(
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static Padding size(@NonNull String padding) {
        int dimen = WidgetUtils.dimenInt(padding);
        return size(dimen, dimen, dimen, dimen);
    }

    public static Padding size(@NonNull String top,
                               @NonNull String bottom,
                               @NonNull String start,
                               @NonNull String end) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom),
                DimenValue.startSize(start),
                DimenValue.endSize(end)
            );
    }

    public static Padding top(int top) {
        return size(
                DimenValue.topSize(top)
            );
    }

    public static Padding bottom(int bottom) {
        return size(
                DimenValue.bottomSize(bottom)
        );
    }

    public static Padding topBottom(int padding) {
        return topBottom(padding, padding);
    }

    public static Padding topBottom(int top, int bottom) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom)
        );
    }

    public static Padding start(int start) {
        return size(
                DimenValue.startSize(start)
        );
    }

    public static Padding end(int end) {
        return size(
                DimenValue.endSize(end)
        );
    }

    public static Padding startEnd(int padding) {
        return startEnd(padding, padding);
    }

    public static Padding startEnd(int start, int end) {
        return size(
                DimenValue.startSize(start),
                DimenValue.endSize(end)
        );
    }

    public static Padding size(int padding) {
        return size(padding, padding, padding, padding);
    }

    public static Padding size(int top, int bottom, int start, int end) {
        return size(
                DimenValue.topSize(top),
                DimenValue.bottomSize(bottom),
                DimenValue.startSize(start),
                DimenValue.endSize(end)
            );
    }

    public static Padding size(@NonNull DimenValue... values) {
        return new Padding(values);
    }

    private Padding(DimenValue... values) {
        super(values);
    }

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper, @NonNull Value[] values) {
        helper.getViewNulls().doNotNull(view -> setPadding(view, values));
    }

    private void setPadding(@NonNull View view,
                            @NonNull Value[] values) {

        int top = view.getPaddingTop();
        int bottom = view.getPaddingBottom();
        int left = view.getPaddingLeft();
        int right = view.getPaddingRight();

        for (Value value : values) {
            if (value instanceof DimenValue) {
                DimenValue dimenValue = (DimenValue) value;
                switch (dimenValue.direction()) {
                    case DimenValue.TOP:
                        top = dimenValue.value();
                        break;
                    case DimenValue.BOTTOM:
                        bottom = dimenValue.value();
                        break;
                    case DimenValue.START:
                        left = dimenValue.value();
                        break;
                    case DimenValue.END:
                        right = dimenValue.value();
                        break;
                    default:
                        break;
                }
            }
        }
        LogHelper.log(TAG, "left=" + left + " top=" + top + " right=" + right + " bottom=" + bottom);
        view.setPadding(left, top, right, bottom);
    }
}
